package com.lmonkey.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebFilter("/shangc/doaddressupdate")
public class DoAddressUpdate implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest reqq = (HttpServletRequest) req;
        HttpServletResponse respp = (HttpServletResponse) resp;
        //设置字符集
        reqq.setCharacterEncoding("UTF-8");
        respp.setContentType("text/html;charset=utf-8");
        PrintWriter out = respp.getWriter();
        String eids = reqq.getParameter("eids");

        String name = reqq.getParameter("name");
        if (name.equals("")) {
            if (eids != null) {
                out.println("<script>alert('请输入姓名');location.href='orderselect?eids=" + eids + "';</script>");
                out.close();
                return;
            } else {
                out.println("<script>alert('请输入姓名');location.href='/lyq/shangc/addressselect';</script>");
                out.close();
                return;
            }
        }

        String mb = reqq.getParameter("userp");
        if (mb.equals("")) {
            if (eids != null) {
                out.println("<script>alert('请输入手机号码');location.href='orderselect?eids=" + eids + "';</script>");
                out.close();
                return;
            } else {
                out.println("<script>alert('请输入手机号码');location.href='/lyq/shangc/addressselect';</script>");
                out.close();
                return;

            }
        } else {
            Pattern pt = Pattern.compile("^[1][3,5,7,8]\\d{9}$");
            Matcher mt = pt.matcher(mb);
            if (eids != null) {
                if (!mt.matches()) {
                    out.println("<script>alert('手机号码格式不正确');location.href='orderselect?eids=" + eids + "';</script>");
                    out.close();
                    return;
                }
            } else {
                if (!mt.matches()) {
                    out.println("<script>alert('手机号码格式不正确');location.href='/lyq/shangc/addressselect';</script>");
                    out.close();
                    return;
                }
            }
        }

        String ar = reqq.getParameter("useraddress");
        if (ar.equals("")){
            if (eids != null) {
                out.println("<script>alert('请输入地址');location.href='orderselect?eids=" + eids + "';</script>");
                out.close();
                return;
            }
            else{
                out.println("<script>alert('请输入地址');location.href='/lyq/shangc/addressselect';</script>");
                out.close();
                return;
            }
        }

        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
